/*
    实现一个字符串分割函数：将一个字符串按照另一个字符串分割
*/
#include <iostream>
#include <string>
#include <vector>

int splitStr(const std::string& str, const std::string& sep, std::vector<std::string>& res)
{
    int begin = 0, end = 0;
    while((end = str.find(sep, begin)) != std::string::npos)
    {
        if(begin != end)
            res.push_back(str.substr(begin, end - begin));
        begin = end + sep.size();
    }
    if(begin < str.size())
        res.push_back(str.substr(begin, str.size() - begin));
    return res.size();
}

int main()
{
    std::string str = "hello world hello hello";
    std::vector<std::string> res;
    int n = splitStr(str, " ", res);
    std::cout << n << std::endl;
    for(auto& str : res)
        std::cout << str << std::endl;

    return 0;
}